package com.wushijia.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * description:服务端
 *
 * @author yang
 * @date 2018/1/7 17:45
 */
public class Server {

  public static void main(String[] args) {
    try {
      ServerSocket serverSocket = new ServerSocket(8080);
      //阻塞等待请求
      Socket socket = serverSocket.accept();
      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      String line = reader.readLine();
      System.out.println("received from client:" + line);
      PrintWriter printWriter = new PrintWriter(socket.getOutputStream());

      printWriter.print("received data:" + line);
      printWriter.flush();
      printWriter.close();

      reader.close();
      socket.close();
      serverSocket.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
